package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.Util.MenuTree;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Menu;
import com.zhao.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    @RequestMapping("data_menu")
    public String queryMenu(Model model){
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("idx+0");
        List<Menu> menus = menuService.list(wrapper);
        Collections.reverse(menus);
        for (Menu menu:menus) {
            String newFather=menu.getFather();
            String[] split = menu.getIdx().split("\\.");
            for (int i = 0; i <split.length-1 ; i++) {
                newFather="∟"+newFather;
            }
            menu.setFather(newFather);
        }
        model.addAttribute("menus",menus);
        return "admin/data_menu";
    }

    @RequestMapping("editMenu/{id}")
    public String editMenu(@PathVariable("id")Integer id, Model model){
        Menu menu = menuService.getOne(new QueryWrapper<Menu>().eq("menu_id", id));
        model.addAttribute("menu",menu);
        return "admin/update_menu";
    }
    //添加界面
    @RequestMapping("addMenu")
    public String addMenu(){
        return "admin/add_menu";
    }
    //添加menu
    //@MyLog(value = "新增菜单")
    @RequestMapping("saveMenu")
    public String saveMenu(Menu menu){
        menuService.saveOrUpdate(menu);
        return "redirect:/menu/data_menu";
    }
    //删除
    //@MyLog(value = "删除菜单")
    @RequestMapping("delMenu/{id}")
    public String delMenu(@PathVariable("id") Integer id){
        menuService.removeById(id);
        return "redirect:/menu/data_menu";
    }
    //图标页面
    @RequestMapping("icon")
    public String icon(){
        return "admin/icon";
    }
    @ResponseBody
    @RequestMapping("fatherMenuTree")
    public Object fatherMenuTree() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("father","0");
        wrapper.orderByAsc("idx");
        List<Menu> menus = menuService.list(wrapper);
        for (Menu menu: menus) {
            QueryWrapper<Menu> wrapper1=new QueryWrapper<>();
            wrapper1.eq("father",menu.getMenuId());
            menu.setChildren(menuService.list(wrapper1));
            if (menu.getChildren().size()>0){
                for (Menu menu1: menu.getChildren()) {
                    QueryWrapper<Menu> wrapper2=new QueryWrapper<>();
                    wrapper2.eq("father",menu1.getMenuId());
                    menu1.setChildren(menuService.list(wrapper2));
                }
            }
        }
        List<HashMap<String, Object>> result = new ArrayList<>();    //定义一个map处理json键名问题
        MenuTree menuTree =new MenuTree();
        return menuTree.addfun(menus, result);
    }
}
